package com.tencent.essbasic.api;

import com.ruoyi.common.exception.GlobalException;
import com.tencent.essbasic.bytemplate.ByTemplate;
import com.tencent.essbasic.common.CreateFlowUtils;
import com.tencent.essbasic.config.Config;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.essbasic.v20210526.EssbasicClient;
import com.tencentcloudapi.essbasic.v20210526.models.*;

import java.util.ArrayList;
import java.util.List;

import static com.tencent.essbasic.common.CreateFlowUtils.setAgent;

public class ChannelCreatePrepareFlow {

    public static ChannelCreatePrepareFlowResponse channelCreatePrepareFlow(String flowName, String flowType, String resourceId, CommonFlowApprover[] approverInfos, Agent agent, Long resourceType, Boolean skipUploadFile) {
        try {
            // 实例化一个client
            EssbasicClient client = CreateFlowUtils.initClient();
            // 实例化一个请求对象,每个接口都会对应一个request对象
            ChannelCreatePrepareFlowRequest req = new ChannelCreatePrepareFlowRequest();

            req.setAgent(agent);

            req.setResourceType(resourceType);
            req.setResourceId(resourceId);

            BaseFlowInfo baseFlowInfo = new BaseFlowInfo();
            baseFlowInfo.setFlowType(flowType);
            baseFlowInfo.setFlowName(flowName);
            baseFlowInfo.setFlowDescription(flowName);

            baseFlowInfo.setApprovers(approverInfos);

            req.setFlowInfo(baseFlowInfo);

            //req.setFlowApproverList(approverInfos);

            CreateFlowOption flowOption = new CreateFlowOption();
            flowOption.setCanEditFlow(true);
            flowOption.setSkipUploadFile(skipUploadFile);

            req.setFlowOption(flowOption);

            return client.ChannelCreatePrepareFlow(req);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            throw new GlobalException(e.getMessage());
        }
    }

    public static void main(String[] args) throws TencentCloudSDKException {

        //Recipient[] recipients = ByTemplate.GetRecipients("yDttFUUckpxxg4hdUxVDlg6vDCJFgYe9", setAgent());

        List<CommonFlowApprover> commonFlowApprovers = new ArrayList<CommonFlowApprover>();

        CommonFlowApprover approver = new CommonFlowApprover();

        approver.setApproverType(0L);
        approver.setOrganizationName("深圳市弹趣科技有限公司");
        //approver.setRecipientId("yDttFUUckpxxg4hpUxVDlg6jssbMxWOO");
        approver.setOrganizationOpenId("C202507211000554409");
        approver.setOpenId("C202507211000557843");
        commonFlowApprovers.add(approver);

        approver = new CommonFlowApprover();
        approver.setApproverType(1L);
        approver.setApproverName("王琦");
        approver.setApproverMobile("18686638295");
        approver.setNotChannelOrganization(false);
        //approver.setRecipientId("yDttFUUckpxxg4hnUxVDlg68n0bl7MrR");
        commonFlowApprovers.add(approver);

        ChannelCreatePrepareFlowResponse channelCreatePrepareFlowResponse = channelCreatePrepareFlow("测试合同0806", "公司治理", "", commonFlowApprovers.toArray(new CommonFlowApprover[0]), setAgent(), 2L, false);

        System.out.println(channelCreatePrepareFlowResponse.getFlowId());
        System.out.println(channelCreatePrepareFlowResponse.getPrepareFlowUrl());
        System.out.println(channelCreatePrepareFlowResponse.getPreviewFlowUrl());
        System.out.println(channelCreatePrepareFlowResponse.getRequestId());

    }

}
